package com.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.HexUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.CardDAO;
import com.modal.CardModel;
import com.pojo.Card;
import com.pojo.Dictionary;
import com.pojo.UserInfo;
import com.util.DESUtil;
import com.util.StringUtil;
import com.util.ValidateHelper;
import com.vo.CardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("cardService")
public class CardService extends ServiceImpl<CardDAO, Card> {

    @Autowired
    private CardDAO cardDAO;

    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private UserInfoService staffService;

    /**
     * 条件获取芯片信息
     * @param page 页数
     * @param limit 每页条数
     * @param xuliehao 序列号
     * @param status 状态
     * @return 芯片列表
     */
    public List<CardVo> getCardList(int page, int limit, String xuliehao, String status, String queryStr){
        List<String> zyNameList = new ArrayList<String>();
        if (ValidateHelper.isNotEmptyString(queryStr)) {
            List<Dictionary> zyList = new ArrayList<Dictionary>();
            if ("gs".equals(queryStr)) {
                zyList = dictionaryService.getDicByZyGs();
            }else if ("jt".equals(queryStr)) {
                zyList = dictionaryService.getDicByZyJt();
            }
            if (ValidateHelper.isNotEmptyCollection(zyList)) {
                for (Dictionary dictionary : zyList) {
                    if (ValidateHelper.isNotEmptyString(dictionary.getName())) {
                        zyNameList.add(dictionary.getName());
                    }
                }
            }
        }
        return cardDAO.findCardByCondition(page, limit, xuliehao, status, queryStr, zyNameList);
    }

    /**
     * 获取3Des
     * @param random 随机数
     * @param xuliehao 序列号
     * @return 密文
     */
    public String get3Des(String random,String xuliehao){

        if(random.length() == 8) {
            random = random + "00000000";
        }
        else if (random.length() == 12) {
            random = random + "0000";
        }
        Card card = super.getOne(new QueryWrapper<Card>().eq("xuliehao" , xuliehao));
        if(null != card){
            String miYao = card.getMiyao();
             return HexUtil.encodeHexStr(DESUtil.desCrypto(HexUtil.decodeHex(random) , HexUtil.decodeHex(miYao)));
        }
        return "";
    }

    public int getCardCount(String xuliehao, String status, String queryStr){
        List<String> zyNameList = new ArrayList<String>();
        if (ValidateHelper.isNotEmptyString(queryStr)) {
            List<Dictionary> zyList = new ArrayList<Dictionary>();
            if ("gs".equals(queryStr)) {
                zyList = dictionaryService.getDicByZyGs();
            }else if ("jt".equals(queryStr)) {
                zyList = dictionaryService.getDicByZyJt();
            }
            if (ValidateHelper.isNotEmptyCollection(zyList)) {
                for (Dictionary dictionary : zyList) {
                    if (ValidateHelper.isNotEmptyString(dictionary.getName())) {
                        zyNameList.add(dictionary.getName());
                    }
                }
            }
        }
        return cardDAO.getCardCount(xuliehao,status, queryStr, zyNameList);
    }

    public Card getCardByCardNo(String cardNo) {
        List<Card> cardList = cardDAO.selectList(new QueryWrapper<Card>().eq("xuliehao" , cardNo)) ;
        if (cardList.size() > 0) {
            return cardList.get(0);
        }
        return null;
    }

    public Card getCardByStaffId(String staffId) {
        List<Card> cardList = cardDAO.selectList(new QueryWrapper<Card>().eq("staff_id" , staffId)) ;
        if (cardList.size() > 0) {
            return cardList.get(0);
        }
        return null;
    }

    public String insertCard(CardModel m) throws Exception {
        Card card = new Card();
        UserInfo staff = staffService.getById(card.getStaffId());
        if(staff == null) {
            throw new Exception("会员不存在");
        }
        staff.setCardNo(card.getXuliehao());
        staff.setPin(card.getMiyao());
        staffService.updateById(staff);
        BeanUtil.copyProperties(m, card);
        if (cardDAO.insert(card) < 1) {
            return "新增card失败";
        }
        return null;
    }

    public String updateCard(CardModel m) throws Exception {
        Card card = cardDAO.selectById(m.getId());
        UserInfo staff = staffService.getById(card.getStaffId());
        if(staff == null) {
            throw new Exception("会员不存在");
        }
        staff.setCardNo(card.getXuliehao());
        staff.setPin(card.getMiyao());
        staffService.updateById(staff);
        if (card == null) {
            return "card不存在";
        }
        BeanUtil.copyProperties(m, card);
        if (cardDAO.updateById(card) < 1) {
            return "编辑card失败";
        }
        return null;
    }

    public String deleteCard(CardModel m){
        if (StringUtil.isNull(m.getId())) {
            return "删除卡的id为空";
        }
        Card card = cardDAO.selectById(m.getId());
        if (card == null) {
            return "card不存在";
        }
        int row = cardDAO.deleteById(m.getId());
        if (row < 1) {
            return "删除卡失败";
        }
        return null;
    }
}